<?php

namespace App\Models\Record\Send;

use Illuminate\Database\Eloquent\Model;

class Sms extends Model
{
    protected $table = 'record_send_sms';

    //每日最多发送验证码次数
    var $maxDayCount = 10;

    //验证码过期时间,单位秒
    var $expireTime = 600; 

    /**
     * 验证手机号时间间隔发送频率
     * @param  [int] $phone  [手机号]
     * @param  [int] $tpl_id [模板ID]
     * @return [type]         [description]
     */
    public function checkThrottle($phone,$tpl_id){
        $throttle = self::where(['phone'=>$phone,'tpl_id'=>$tpl_id])->orderBy('created_at','desc')->first();
        if($throttle && (time()-strtotime($throttle->created_at))<60){
            return ['code'=>1,'msg'=>'短信发送间隔过快,60秒后再试'];
        }
        return ['code' => 0];
    }

    /**
     * 验证手机号每日模板发送次数
     * @param  [int] $phone  [手机号]
     * @param  [int] $tpl_id [模板ID]
     * @return [type]         [description]
     */
    public function checkDayThrottle($phone,$tpl_id){
        $dayCount = self::where(['phone'=>$phone,'tpl_id'=>$tpl_id,'errorCode'=>0,'createDay'=>date('Y-m-d')])->count();
        if($dayCount>=$this->maxDayCount){
            return ['code'=>1,'msg'=>'手机每日发送验证码次数已达上线'];  
        }
        return ['code' => 0];
    }
}
